#include <kieper/PacketHeader.hpp>

#include <boost/cstdint.hpp>

namespace kieper {
PacketHeader::PacketHeader() {}
PacketHeader::PacketHeader(uint16_t protocol, uint16_t messageType,
  uint32_t packetId) : protocol_(protocol), messageType_(messageType),
  packetId_(packetId) {}

PacketHeader::PacketHeader(unsigned char* array) {
    fromArray(array);
}

PacketHeader::~PacketHeader() {}

void PacketHeader::toArray(unsigned char* array) const {
    uint16_t* array_uint16 = reinterpret_cast<uint16_t*>(array);
    array_uint16[0] = protocol_;
    array_uint16[1] = messageType_;

    uint32_t* array_uint32 = reinterpret_cast<uint32_t*>(array);
    array_uint32[1] = packetId_;
}

void PacketHeader::fromArray(const unsigned char* array) {
    //TODO: network->host bo
    const uint16_t* array_uint16 = reinterpret_cast<const uint16_t*>(array);
    protocol_ = array_uint16[0];
    messageType_ = array_uint16[1];

    const uint32_t* array_uint32 = reinterpret_cast<const uint32_t*>(array);
    packetId_ = array_uint32[1];
}


uint16_t PacketHeader::protocol() {
    return protocol_;
}

uint16_t PacketHeader::messageType() {
    return messageType_;
}

uint32_t PacketHeader::packetId() {
    return packetId_;
}

}
